﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using System.Text.RegularExpressions;
using QuickJob.lib;

namespace QuickJob.forms
{
   public class ListItemPicture : UserControl
    {

       /// <summary>
       /// 图片路径
       /// </summary>
       public string imgDir;


       /// <summary>
       /// 图像
       /// </summary>
       public Image img;

       /// <summary>
       /// 标题
       /// </summary>
        public string title;


        Config _config;

        string kuanHao = "未设置";


       /// <summary>
       /// 网址
       /// </summary>
        public string url;
        private PictureBox pictureBox;
        private LinkLabel linkLabel_link;
        private Label label_title;
        private Label label_Dir;
        private TextBox textBox1;
        private Label label1;
        private ComboBox comboBox_pinming;
        private Label label2;
        private Button button1;


       //选择
        bool selectFlag = false;



        #region 构造

        public ListItemPicture():base() { 
        
            InitializeComponent();
                
        }
        public ListItemPicture(string imgdir,Config config) {

            InitializeComponent();

            this.pictureBox.Click += pictureBox_Click;
            this.MouseDoubleClick += ListItemPicture_MouseDoubleClick;
            this.label_Dir.MouseDoubleClick += ListItemPicture_MouseDoubleClick;
            this.label_title.MouseDoubleClick += ListItemPicture_MouseDoubleClick;



            _config = config;

            imgDir = imgdir;

            //加载数据
            do
            {



                //加载图片
                try
                {
                    string f = "";
                    string[] files = System.IO.Directory.GetFiles(imgdir);
                    if (files == null || files.Length <= 0) break;
                    for (int i = 0; i < files.Length; i++)
                    {
                        f = files[i];
                        if (f.Contains("jpg") || f.Contains("png") || f.Contains("jpeg")) break;
                    }
                    string newPath = System.IO.Path.Combine(imgdir, f);
                    img = Image.FromFile(newPath);
                    pictureBox.Image = img;

                }
                catch
                {
                    pictureBox.Image.Dispose();
                }


                //加载　信息
                try
                {
                    string[] files = System.IO.Directory.GetFiles(imgdir);
                    string f = null;
                    for (int i = 0; i < files.Length; i++)
                    {

                        if (files[i].Contains("html"))
                        {

                            f = files[i];
                            break;
                        }
                    }
                    if (f != null)
                    {
                        string newPath = System.IO.Path.Combine(imgdir, f);
                        System.IO.FileStream fs = System.IO.File.OpenRead(newPath);
                        byte[] fsdata = new byte[fs.Length];
                        int r = fs.Read(fsdata, 0, fsdata.Length);
                        string fsdatastring = System.Text.Encoding.Default.GetString(fsdata);

                        Regex re = new Regex("<a href=(\\S)*\">");
                        string title = re.Match(fsdatastring).Value;
                        string url = title.Replace("<a href=\"", "").Replace("\">", "");
                        title = Regex.Match(fsdatastring, @">([^<]\S|\s|\d)*</a>").Value.Replace(">", "").Replace("</a","");


                        this.label_title.Text = title;
                        this.linkLabel_link.Text = url;
                        this.linkLabel_link.Click += linkLabel_link_Click;

                        this.label_Dir.Text = imgdir;
                    }
                }
                catch { }


                //////////////////////////////////////////////////////////////////////////
                //加载选项
                ReLoadConfig(config);


            } while (false); 
            


        }

        void ListItemPicture_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (selectFlag == true) selectFlag = false;
            else selectFlag = true;
            OnCheckedHandler(selectFlag);
        }


        void pictureBox_Click(object sender, EventArgs e)
        {

            if (selectFlag == true) selectFlag = false;
            else selectFlag = true;
            OnCheckedHandler(selectFlag);
        }

        void linkLabel_link_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(this.linkLabel_link.Text);
        }
        #endregion



#region 操作
       /// <summary>
       /// 重新加载
       /// </summary>
       /// <param name="config"></param>
        public void ReLoadConfig(Config config)
        {
            try
            {
                int index = this.comboBox_pinming.SelectedIndex;
                this.comboBox_pinming.Items.Clear();
                foreach (DropConfigItem i in config.clist)
                {
                    comboBox_pinming.Items.Add(i.ConfigName);
                }
                comboBox_pinming.SelectedIndex = index;

            }
            catch { }

        }


       /// <summary>
       /// 保存图片
       /// </summary>
        public void SaveImage()
        {
            try
            {
                PictureBox pb = new PictureBox();
                DropConfigItem i = _config.clist[comboBox_pinming.SelectedIndex].Clone();
                i.KuanHao.text = kuanHao;
                DropConfigView dcv = new DropConfigView(i, pb);
                dcv.SaveBmp(imgDir, "合格证.jpg");
                pb.Dispose();
                
            }
            catch { }
        }


        public void SaveImageExt()
        {
            try
            {
                DropConfigItem i = _config.clist[comboBox_pinming.SelectedIndex].Clone();
                i.KuanHao.text = kuanHao;
                PreVies pv = new PreVies(i);
                pv.Show();
                this.Focus();
                pv.SaveImage(imgDir, "合格证.jpeg");
                pv.Close();

            }
            catch { }           
        }

        /// <summary>
        /// 是否勾选
        /// </summary>
        /// <returns></returns>
        public bool Checked()
        {
            return selectFlag;
        }

       /// <summary>
       /// 设置　勾选
       /// </summary>
       /// <param name="c"></param>
       /// <returns></returns>
        public bool SetChecked(bool c)
        {
            selectFlag = c;
            OnCheckedHandler(selectFlag);
            return c;
        }


        /// <summary>
        /// 设置　配置项
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public bool SetConfig(int index)
        {
            try
            {
                comboBox_pinming.SelectedIndex = index;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool SetPinMing(string pin)
        {
            ComboBox.ObjectCollection names = comboBox_pinming.Items;
            if(names.Count<0)return false;
            {
                for(int i= 0;i<names.Count;i++){
                    string n = (string)names[i];
                    if (n.Contains(pin))
                    {
                        SetConfig(i);
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 设置　款号
        /// </summary>
        /// <param name="KuanHao"></param>
        /// <returns></returns>
        public bool SetKuanHao(string KuanHao)
        {
            textBox1.Text = KuanHao;
            kuanHao = KuanHao;
            return true;
        }
#endregion


        #region 
        private void InitializeComponent()
        {
            this.linkLabel_link = new System.Windows.Forms.LinkLabel();
            this.label_title = new System.Windows.Forms.Label();
            this.label_Dir = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox_pinming = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.pictureBox = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
            this.SuspendLayout();
            // 
            // linkLabel_link
            // 
            this.linkLabel_link.AutoSize = true;
            this.linkLabel_link.Location = new System.Drawing.Point(137, 4);
            this.linkLabel_link.Name = "linkLabel_link";
            this.linkLabel_link.Size = new System.Drawing.Size(29, 12);
            this.linkLabel_link.TabIndex = 1;
            this.linkLabel_link.TabStop = true;
            this.linkLabel_link.Text = "链接";
            // 
            // label_title
            // 
            this.label_title.AutoSize = true;
            this.label_title.Location = new System.Drawing.Point(137, 30);
            this.label_title.Name = "label_title";
            this.label_title.Size = new System.Drawing.Size(29, 12);
            this.label_title.TabIndex = 2;
            this.label_title.Text = "标题";
            // 
            // label_Dir
            // 
            this.label_Dir.AutoSize = true;
            this.label_Dir.Location = new System.Drawing.Point(137, 54);
            this.label_Dir.Name = "label_Dir";
            this.label_Dir.Size = new System.Drawing.Size(29, 12);
            this.label_Dir.TabIndex = 3;
            this.label_Dir.Text = "路径";
            // 
            // textBox1
            // 
            this.textBox1.Font = new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.textBox1.Location = new System.Drawing.Point(147, 95);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 26);
            this.textBox1.TabIndex = 4;
            this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(145, 80);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 12);
            this.label1.TabIndex = 5;
            this.label1.Text = "款号";
            // 
            // comboBox_pinming
            // 
            this.comboBox_pinming.Font = new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.comboBox_pinming.FormattingEnabled = true;
            this.comboBox_pinming.Location = new System.Drawing.Point(269, 95);
            this.comboBox_pinming.Name = "comboBox_pinming";
            this.comboBox_pinming.Size = new System.Drawing.Size(91, 24);
            this.comboBox_pinming.TabIndex = 6;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(267, 80);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(29, 12);
            this.label2.TabIndex = 8;
            this.label2.Text = "品名";
            // 
            // button1
            // 
            this.button1.Font = new System.Drawing.Font("黑体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.button1.Location = new System.Drawing.Point(424, 92);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(60, 28);
            this.button1.TabIndex = 10;
            this.button1.Text = "预览";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // pictureBox
            // 
            this.pictureBox.Location = new System.Drawing.Point(4, 4);
            this.pictureBox.Name = "pictureBox";
            this.pictureBox.Size = new System.Drawing.Size(125, 125);
            this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox.TabIndex = 0;
            this.pictureBox.TabStop = false;
            // 
            // ListItemPicture
            // 
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.comboBox_pinming);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.label_Dir);
            this.Controls.Add(this.label_title);
            this.Controls.Add(this.linkLabel_link);
            this.Controls.Add(this.pictureBox);
            this.Name = "ListItemPicture";
            this.Size = new System.Drawing.Size(563, 134);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion




        /// <summary>
       /// 点击　预览
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                DropConfigItem i = _config.clist[comboBox_pinming.SelectedIndex].Clone();
                i.KuanHao.text = kuanHao;
                PreVies pv = new PreVies(i);
                pv.Show();
               
            }
            catch { }
        }


       /// <summary>
       /// 修改　款号
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            kuanHao = textBox1.Text;
        }


       Color oldcolor = DefaultBackColor;
       void OnCheckedHandler(bool c){

           if (c == true)
           {

               this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
               this.BackColor = Color.FromArgb(0xa3, 0xf4, 0xfb);
           }
           else
           {
               this.BorderStyle = System.Windows.Forms.BorderStyle.None;
               this.BackColor = oldcolor;
           }
            
        }



    }
}
